package com.eoekun.dashboard.contoller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

/**
 * 参数字符串处理
 * 
 * @see http://visionjava1.appspot.com/content/spring-mvc-change-default-empty-string-submission-to-null-using-initbinder.html
 * @author chenkun
 * @createTime 2019年4月15日 上午11:27:51
 */
@ControllerAdvice
public class StringTrimmerAdvice {

    private static final Logger log = LoggerFactory.getLogger(StringTrimmerAdvice.class);

    @InitBinder
    public void allowEmptyDateBinding(WebDataBinder binder) {
        // tell spring to set empty values as null instead of empty string.
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
        log.debug("init paramAdvice success!");
    }
}
